package com.mock.ems.controller;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.DateUtil;
import com.mock.base.util.StringUtil;
import com.mock.ems.common.JsonResult;
import com.mock.ems.service.MenuService;
import com.mock.ems.service.MenuServiceImpl;

import java.util.Date;

/**
 * Created by lichengming on 2017/6/14.
 */
public class HomeController extends Controller {

    MenuService menuService = new MenuServiceImpl();

    public void index() {

        Record admin = getSessionAttr("admin");
        String menuJson = JSON.toJSONString(menuService.getMenuListByUserId(admin.getLong("id")));
        setAttr("menuJson",menuJson);
        renderJsp("/home.jsp");
    }


    public void logout() {
        getSession().invalidate();
        renderJsp("/login.jsp");
    }

    public void toLogin() {
        getSession().invalidate();
        renderJsp("/login.jsp");
    }

    public void show() {


        String currentDayRecordSQL = "select " +
                " IFNULL(sum(amount),0.00) as day_amount," +
                " IFNULL(sum(income),0.00) as day_income," +
                " IFNULL(sum(outcome),0.00) as day_outcome" +
                " from distribution_platform_detail where create_time >= ? and create_time < ? ";
        Record currentDayRecord = Db.findFirst(currentDayRecordSQL,
                DateUtil.getCurrentBeginTime(),
                DateUtil.getCurrentEndTime());

        if (null != currentDayRecord) {
            setAttr("currentDayPlatformRecord",currentDayRecord.getColumns());
        }

        String totalDayRecordSQL = "select " +
                " IFNULL(sum(amount),0.00) as total_amount," +
                " IFNULL(sum(income),0.00) as total_income," +
                " IFNULL(sum(outcome),0.00) as total_outcome" +
                " from distribution_platform_detail";
        Record totalPlatformRecord = Db.findFirst(totalDayRecordSQL);
        if (null != totalPlatformRecord) {
            setAttr("totalPlatformRecord",totalPlatformRecord.getColumns());
        }

        //消费者
        String currentDayConsumerSQL = "select sum(income) as day_consumer_income " +
                " from distribution_consumer_detail where create_time >= ? and create_time < ? ";
        Double currentDayConsumerIncome = Db.queryDouble(currentDayConsumerSQL,DateUtil.getCurrentBeginTime(),
                DateUtil.getCurrentEndTime());
        if (null == currentDayConsumerIncome) currentDayConsumerIncome = 0.00;
        setAttr("current_day_consumer_income",currentDayConsumerIncome);


        String totalConsumerSQL = "select sum(income) as total_consumer_income from distribution_consumer_detail";
        Double totalConsumerIncome = Db.queryDouble(totalConsumerSQL);
        if (null == totalConsumerIncome) totalConsumerIncome = 0.00;
        setAttr("total_consumer_income",totalConsumerIncome);


        //消费商
        String currentDayRecommanderSQL = "select sum(income) as day_recommander_income " +
                " from distribution_recommander_detail where create_time >= ? and create_time < ? ";
        Double currentDayRecommanderIncome = Db.queryDouble(currentDayRecommanderSQL,
                DateUtil.getCurrentBeginTime(),
                DateUtil.getCurrentEndTime());
        if (null == currentDayRecommanderIncome) currentDayRecommanderIncome = 0.00;
        setAttr("current_day_recommander_income",currentDayRecommanderIncome);


        String totalRecommanderSQL = "select sum(income) as total_recommander_income from distribution_recommander_detail";
        Double totalRecommanderIncome = Db.queryDouble(totalRecommanderSQL);
        if (null == totalRecommanderIncome) totalRecommanderIncome = 0.00;
        setAttr("total_recommander_income",totalRecommanderIncome);


        //品牌合作伙伴
        String currentDayCooperationPartnerSQL = "select sum(income) as day_cooperation_partner_income " +
                " from distribution_cooperation_partner_detail where create_time >= ? and create_time < ? ";
        Double currentDayCooperationPartnerIncome = Db.queryDouble(currentDayCooperationPartnerSQL,
                DateUtil.getCurrentBeginTime(),
                DateUtil.getCurrentEndTime());
        if (null == currentDayCooperationPartnerIncome) currentDayCooperationPartnerIncome = 0.00;
        setAttr("current_day_cooperation_partner_income",currentDayCooperationPartnerIncome);


        String totalCooperationPartnerSQL = "select sum(income) as day_cooperation_partner_income " +
                " from distribution_cooperation_partner_detail";
        Double totalCooperationPartnerIncome = Db.queryDouble(totalCooperationPartnerSQL);
        if (null == totalCooperationPartnerIncome) totalCooperationPartnerIncome = 0.00;
        setAttr("total_cooperation_partner_income",totalCooperationPartnerIncome);



        //品牌服务中心
        String currentDayServiceCenterSQL = "select sum(income) as day_service_center_income " +
                " from distribution_service_center_detail where create_time >= ? and create_time < ? ";
        Double currentDayServiceCenterIncome = Db.queryDouble(currentDayServiceCenterSQL,
                DateUtil.getCurrentBeginTime(),
                DateUtil.getCurrentEndTime());
        if (null == currentDayServiceCenterIncome) currentDayServiceCenterIncome = 0.00;
        setAttr("current_day_service_center_income",currentDayServiceCenterIncome);


        String totalServiceCenterSQL = "select sum(income) as day_service_center_income " +
                " from distribution_service_center_detail";
        Double totalServiceCenterIncome = Db.queryDouble(totalServiceCenterSQL);
        if (null == totalServiceCenterIncome) totalServiceCenterIncome = 0.00;
        setAttr("total_service_center_income",totalServiceCenterIncome);


        //获取商家统计信息
        String currentDayMerchantSQL = "select " +
                " IFNULL(sum(total_amount),0.00) as day_merchant_amount," +
                " IFNULL(sum(total_income),0.00) as day_merchant_income," +
                " IFNULL(sum(total_score),0.00) as day_merchant_score," +
                " IFNULL(sum(total_outcome),0.00) as day_merchant_outcome" +
                " from distribution_merchant_day_count where time >= ? and time < ? ";
        Record currentDayMerchant = Db.findFirst(currentDayMerchantSQL,
                DateUtil.toSeconds(DateUtil.getCurrentBeginTime().getTime()),
                DateUtil.toSeconds(DateUtil.getCurrentEndTime().getTime()));
        if (null != currentDayMerchant) {
            setAttr("currentDayMerchantRecord",currentDayMerchant.getColumns());
        }


        String totalMerchantSQL = "select " +
                " IFNULL(sum(total_amount),0.00) as total_merchant_amount," +
                " IFNULL(sum(total_income),0.00) as total_merchant_income," +
                " IFNULL(sum(total_score),0.00) as total_merchant_score," +
                " IFNULL(sum(total_outcome),0.00) as total_merchant_outcome" +
                " from distribution_merchant_day_count";
        Record totalMerchant = Db.findFirst(totalMerchantSQL);
        if (null != totalMerchant) {
            setAttr("totalMerchantRecord",totalMerchant.getColumns());
        }
        renderJsp("/index.jsp");
    }



    public void login() {

        JsonResult result = new JsonResult();
        String userName = getPara("userName");
        String password = getPara("password");
        if (StringUtil.isEmpty(userName)) {
            renderJson(result.status("-1").show("用户名不能为空"));
            return;
        }

        if (StringUtil.isEmpty(password)) {
            renderJson(result.status("-2").show("密码不能为空"));
            return;
        }

        String querySQL = "select * from ems_admin where login_account = ? ";
        Record record = Db.findFirst(querySQL,userName);
        if (null == record) {
            renderJson(result.status("-3").show("用户名或密码不正确"));
            return;
        }

        String pass = record.getStr("password");
        if (!password.equals(pass)) {
            renderJson(result.status("-4").show("用户名或密码不正确"));
            return;
        }

        //更新
        record.set("last_login_time",new Date());
        Db.update("ems_admin",record);

        //登录通过
        setSessionAttr("admin",record);
        //一小时过期
        getSession().setMaxInactiveInterval(3600);
        renderJson(result.status("000"));
        return;
    }
}
